//
//  ERSysButton.m
//  EnjoyRunning
//
//  Created by ljm on 2017/4/19.
//  Copyright © 2017年 ljm. All rights reserved.
//

#import "ERSysButton.h"


@implementation EMSysButton {
    UIColor *_bkCol;
    UIColor *_titleCol;
}

-(instancetype)initWithFrame:(CGRect)frame withTag:(NSInteger)tag withTitle:(NSString *)title withTitleColor:(UIColor *)titleColor withBackgrougdColor:(UIColor *)bgColor withCornerRadious:(CGFloat)corner withClickedBlock:(ButtomClickedWithSender)clicked {
    //corner：0 -- 0.5
    self = [super initWithFrame:frame];
    if (self) {
        if (clicked) {
            self.btnClicked = clicked;
            [self addTarget:self
                     action:@selector(btnTouchupInside:) forControlEvents:UIControlEventTouchUpInside];
        }
        self.tag = tag;
        [self setTitle:title
              forState:UIControlStateNormal];
        [self setTitleColor:titleColor
                   forState:UIControlStateNormal];
//        self.titleLabel.font = ManYu_Font;
        self.titleLabel.adjustsFontSizeToFitWidth = YES;
        [self setBackgroundColor:bgColor];
        self.layer.cornerRadius = self.frame.size.height*corner;
    }
    return self;
}


-(instancetype)initWithFrame:(CGRect)frame withImgName:(NSString*)img withClickedBlock:(ButtomClickedWithSender)clicked{
    self = [super initWithFrame:frame];
    if (self) {
        if (clicked) {
            self.btnClicked = clicked;
            [self addTarget:self
                     action:@selector(btnTouchupInside:) forControlEvents:UIControlEventTouchUpInside];
        }
        [self setImage:[UIImage imageNamed:img] forState:normal];
        
        
    }
    return self;
}


- (instancetype)initWithFrame:(CGRect)frame withImgName:(NSString *)img WithTag:(NSInteger)tag WithCornerRadious:(CGFloat)corner withClickedBlock:(ButtomClickedWithSender)clicked {
    
    self = [super initWithFrame:frame];
    if (self) {
        if (clicked) {
            self.btnClicked = clicked;
            [self addTarget:self
                     action:@selector(btnTouchupInside:) forControlEvents:UIControlEventTouchUpInside];
        }
        
        self.tag = tag;
        [self setImage:[UIImage imageNamed:img] forState:normal];
        self.layer.cornerRadius = self.frame.size.height*corner;
        self.layer.masksToBounds = YES;
        
    }
    return self;
    
}

- (instancetype)initCountdownBtnWithFrame:(CGRect)frame WithTitleColor:(UIColor *)titleCol WithBKColor:(UIColor *)BKColor WithClickedBlock:(ButtomClickedWithSender)clicked {
    
    if ([super initWithFrame:frame]) {
        if (clicked) {
            self.btnClicked = clicked;
            [self addTarget:self
                     action:@selector(countdownBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
        }
        [self setTitle:@"发送验证码" forState:UIControlStateNormal];
//        self.titleLabel.font = ManYu_Font;
        self.titleLabel.adjustsFontSizeToFitWidth = YES;
        [self setTitleColor:titleCol forState:UIControlStateNormal];
        
        [self setBackgroundColor:BKColor];
        self.layer.cornerRadius = self.frame.size.height*0.1;
        
        _bkCol = BKColor;
        _titleCol = titleCol;
        
    }
    return self;
}

-(void)btnTouchupInside:(UIButton *)sender {
    self.btnClicked(sender);
}

- (void)countdownBtnClicked:(UIButton *)sender {
    
    
    self.btnClicked(sender);
    
    __block NSInteger outTime = 19;
    __weak typeof(self) weakSelf = self;
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    
    //每秒执行一次
    dispatch_source_t timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
    dispatch_source_set_timer(timer, dispatch_walltime(NULL, 0), 1.0*NSEC_PER_SEC, 0);
    dispatch_source_set_event_handler(timer, ^{
        
        if (outTime <= 0) {
            dispatch_source_cancel(timer);
            
            dispatch_async(dispatch_get_main_queue(), ^{
                
                [weakSelf setTitle:@"发送验证码" forState:UIControlStateNormal];
                [weakSelf setTitleColor:_titleCol forState:normal];
                [weakSelf setBackgroundColor:_bkCol];
                weakSelf.userInteractionEnabled = YES;
                
            });
        } else {
            int seconds = outTime % 60;
            NSString *strTime = [NSString stringWithFormat:@"%.2d秒后可重发",seconds];
            
            dispatch_async(dispatch_get_main_queue(), ^{
                weakSelf.titleLabel.text = strTime;
                [weakSelf setTitle:strTime forState:UIControlStateNormal];
                weakSelf.backgroundColor = [UIColor clearColor];
                weakSelf.userInteractionEnabled = NO;
            });
            outTime --;
        }
    });
    dispatch_resume(timer);
    
}

@end

